const express = require("express");
const router = express.Router();
const {success, successWithData} = require("../utils/result")
const { check } = require('express-validator');
const {checkParam}  = require("../utils/checkUtils")
const BlogCarousel = require("../db/model/blogCarouselModel");

/**
 * 分页查询接口
 * @api {POST} /blogCarousel/queryBlogCarouselListByPage 分页查询接口
 * @apiDescription 分页查询接口
 * @apiName queryBlogCarouselListByPage
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} page=1   页码
 * @apiBody {Number} limit=10   每页条数
 * @apiParamExample {json} Request-Example:
 *     {
 *        "page": 1,
 *        "limit": "10",
 *     }
 * @apiGroup BlogCarousel
 * @apiVersion 1.0.0
 */
router.post("/blogCarousel/queryBlogCarouselListByPage", check('page').not().isEmpty(),check('limit').not().isEmpty(), async function (req, res) {
  checkParam(req)
  const { page, limit, type } = req.body;
  const where = {};
  let offset = (page - 1) * limit;
  if (type) {
    where["type"] = type;
  }
  const result = await BlogCarousel.findAndCountAll({
    limit,
    offset,
    where,
    order: [["created_time", "DESC"]],
  });
  return res.json(successWithData(result));
});


/**
 * @api {POST} /blogCarousel/add 添加
 * @apiDescription 添加
 * @apiName add
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   
 * @apiBody {String} img_url=''   
 * @apiBody {String} jump_url=''   
 * @apiBody {String} title=''   
 * @apiBody {Number} sort=1   
 * @apiBody {String} created_time='1994-04-01'   
 * @apiBody {String} updated_time='1994-04-01'   
 * @apiBody {Number} status=1   
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *        "img_url": '',
 *        "jump_url": '',
 *        "title": '',
 *        "sort": 1,
 *        "created_time": '1994-04-01',
 *        "updated_time": '1994-04-01',
 *        "status": 1,
 *     }
 * @apiGroup BlogCarousel
 * @apiVersion 1.0.0
 */
router.post("/blogCarousel/add",  async function (req, res) {
  await BlogCarousel.create({...req.body});
  return res.json(success());
});


/**
 * @api {POST} /blogCarousel/edit 编辑
 * @apiDescription 编辑
 * @apiName edit
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   
 * @apiBody {String} img_url=''   
 * @apiBody {String} jump_url=''   
 * @apiBody {String} title=''   
 * @apiBody {Number} sort=1   
 * @apiBody {String} created_time='1994-04-01'   
 * @apiBody {String} updated_time='1994-04-01'   
 * @apiBody {Number} status=1   
 * @apiParamExample {json} Request-Example:
 *     {
 *        "img_url": '',
 *        "jump_url": '',
 *        "title": '',
 *        "sort": 1,
 *        "created_time": '1994-04-01',
 *        "updated_time": '1994-04-01',
 *        "status": 1,
 *     }
 * @apiGroup BlogCarousel
 * @apiVersion 1.0.0
 */
router.post("/blogCarousel/edit", check('id').not().isEmpty(),async function (req, res) {
  checkParam(req)
  await BlogCarousel.update({...req.body}, {where: {id:req.body.id}});
  return res.json(success());
});

/**
 * @api {POST} /blogCarousel/del 删除
 * @apiDescription 删除
 * @apiName del
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   主键id
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *     }
 * @apiGroup BlogCarousel
 * @apiVersion 1.0.0
 */
router.post("/blogCarousel/del", check('id').not().isEmpty(),async function (req, res) {
  checkParam(req)
  await BlogCarousel.destroy({where: {id:req.body.id}});
  return res.json(success());
});
module.exports = router;